package com.superteam.zzl.view;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.superteam.zzl.Des.NewFragment;
import com.superteam.zzl.activity.R;
import com.superteam.zzl.activity.databinding.DestinationfragmentLayoutBinding;

import java.lang.reflect.Field;
import java.util.ArrayList;

/**
 * Created by zhang on 2017/1/6.
 * 目的地
 */

public class MainDestinationFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener{

    private ArrayList<NewFragment> fragments;
    private RadioGroup rg;
    private FragmentManager manager;
    private int currIndex,nextIndex=0;
    private ArrayList<Integer> ids;
   private DestinationfragmentLayoutBinding des;
    public static MainDestinationFragment getInstance(String type) {
        Bundle bundle = new Bundle();
        bundle.putString("type",type);
        MainDestinationFragment fragment = new MainDestinationFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        des = DataBindingUtil.inflate(inflater, R.layout.destinationfragment_layout, container, false);
        //Log.e("data",nextIndex+"");
        return des.getRoot();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        int index=ids.indexOf(checkedId);
        //Log.e("data",index+"....onChecked");
        //验证当前索引和将要显示的索引是否不同
        if(currIndex!=index){
            nextIndex=index;
            changeFragment();
            //  changeRB();
        }
    }

    private void init() {
        //amb= DataBindingUtil.setContentView(this,R.layout.activity_main);

        rg=des.destRg;
        ids=getIds("rb_");
        manager=getChildFragmentManager();
        rg.setOnCheckedChangeListener(this);
        initFragment();
        currIndex =0;
        RadioButton rb=(RadioButton) rg.getChildAt(currIndex);
        rb.setChecked(true);
        rg.setOnCheckedChangeListener(this);
    }

    private void initFragment() {
        fragments=new ArrayList<>();
        for (int i = 0; i <7; i++) {
            fragments.add(NewFragment.getInstance(i));
        }
        currIndex=0;
        FragmentTransaction trans=manager.beginTransaction();
        trans.add(R.id.ll,fragments.get(currIndex));
        trans.commit();
        //Log.e("data","init"+nextIndex);
    }

    private void changeRB() {
        RadioButton rb1=(RadioButton) rg.getChildAt(currIndex);
        RadioButton rb2=(RadioButton) rg.getChildAt(nextIndex);
        Log.e("2323",rb1.getText().toString());
        Log.e("2323",rb2.getText().toString());
        currIndex=nextIndex;
    }

    private void changeFragment()
    {
        //Log.e("data",nextIndex+"");

        Fragment f1=fragments.get(currIndex);//获取当前正在显示的碎片对象
        Fragment f2=fragments.get(nextIndex);//获取将要显示的碎片对象
        //获取碎片事务类对象
        FragmentTransaction transaction=manager.beginTransaction();

        if(f2.isAdded())
        {
            //隐藏当前显示，显示将要显示的碎片
            transaction.hide(f1).show(f2);

        }else
        {
            //隐藏当前显示的，替换将要显示的
            transaction.hide(f1).add(R.id.ll,f2);
        }
        transaction.commit();//提交，确认改变
        currIndex=nextIndex;
    }







    private ArrayList<Integer> getIds(String msg)
    {
        Class<R.id> myClass=R.id.class;
        Field[] arrf=myClass.getDeclaredFields();
        ArrayList<Integer> list=new ArrayList<>();
        for (Field field : arrf) {
            if(field.getName().startsWith(msg))
            {
                try {
                    list.add(field.getInt(null));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        //Log.e("data",list.size()+"List");
        return  list;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            init();
        }
    }
}
